﻿#include "mythread.h"
#include <ctime>
MyThread::MyThread()
{
    qsrand(time(0));
}

MyThread::MyThread(QTcpSocket *psocket, QObject *parent): parent(parent)
{
    tcp_send_receive = psocket;  //刚才定义的tcp去接收这个参数，
    connect(tcp_send_receive,SIGNAL(readyRead()),this,SLOT(resiveMsg()));
}

MyThread::~MyThread()
{

}

void MyThread::run()
{
    while(1)
    {
        QThread::msleep(1000);
    }
}

void MyThread::deal_sendHistory(QString s)
{
    //mutex.lock();
    CJsonObject jsonObj;
    jsonObj.Add("type",QString("4").toStdString());
    jsonObj.Add("history",s.toStdString());
    QString retData(jsonObj.ToString().c_str());
    tcp_send_receive->write(retData.toUtf8());
   // tcp_send_receive->write(s.toUtf8());
    //mutex.unlock();
}

void MyThread::deal_sendreciteWord(QList<QString> list)
{
   // tcp_send_receive->write(s.toUtf8());

    int n = qrand() % list.size();
    CJsonObject jsonObj;
    jsonObj.Add("type",QString("6").toStdString());
    jsonObj.Add("reciteWord",list.at(n).toStdString());
    QString retData(jsonObj.ToString().c_str());
    tcp_send_receive->write(retData.toUtf8());
    //tcp_send_receive->write(list.at(n).toUtf8());
    //qDebug()<<list.at(i);
}
void MyThread::resiveMsg()
{
     QByteArray mess = tcp_send_receive->readAll();
     //QString deal_data = mess.toStdString();
     CJsonObject  jsonObj(mess.toStdString());
     std::string stdStr;

     jsonObj.Get("type",stdStr);
     QString type = QString(stdStr.c_str());
     jsonObj.Get("name",stdStr);
     sql->username = QString(stdStr.c_str());
     qDebug()<<sql->username;
     jsonObj.Get("password", stdStr);
     sql->password = QString(stdStr.c_str());
     jsonObj.Get("word",stdStr);
     word = QString(stdStr.c_str());
     jsonObj.Get("exp",stdStr);
     QString exp = QString(stdStr.c_str());
     qDebug()<<"read_Msg"<<word;
     if(type == "1")
     {
         CJsonObject jsonObj;
         jsonObj.Add("type",QString("1").toStdString());
         jsonObj.Add("register",sql->insertUser().toStdString());
         QString retData(jsonObj.ToString().c_str());
         tcp_send_receive->write(retData.toUtf8());
//         tcp_send_receive->write(sql->insertUser().toUtf8());
         emit sendMsg(type,sql-> username, sql->password);
     }
     else if(type == "2")
     {

         CJsonObject jsonObj;
         jsonObj.Add("type",QString("2").toStdString());
         jsonObj.Add("login",sql->isPasswordRight().toStdString());
         QString retData(jsonObj.ToString().c_str());
         tcp_send_receive->write(retData.toUtf8());
//         tcp_send_receive->write(sql->isPasswordRight().toUtf8());
         emit sendMsg(type,sql-> username, sql->password);
     }
     else if(type == "3")
     {
//         qDebug()<<type;
//         tcp_send_receive->write(sql->findWords(word).toUtf8());
         CJsonObject jsonObj;
         jsonObj.Add("type",QString("3").toStdString());
         jsonObj.Add("exp",sql->findWords(word).toStdString());
         QString retData(jsonObj.ToString().c_str());
         tcp_send_receive->write(retData.toUtf8());
     }
     else if (type == "4")
     {
         qDebug()<<type;
         sql->selectHistory();
         connect(sql,SIGNAL(sendHistory(QString)),this,SLOT(deal_sendHistory(QString)));
     }
     else if(type == "5")
     {
         // tcp_send_receive->write(sql->addWords(word).toUtf8());
         CJsonObject jsonObj;
         jsonObj.Add("type",QString("5").toStdString());
         jsonObj.Add("addWords",sql->addWords(word).toStdString());
         QString retData(jsonObj.ToString().c_str());
         tcp_send_receive->write(retData.toUtf8());
     }
     else if(type == "6")
     {
         sql->reciteWord();
         connect(sql,SIGNAL(sendreciteWord(QList<QString>)),this,SLOT(deal_sendreciteWord(QList<QString>)));
     }
     else if(type == "7")
     {
         CJsonObject jsonObj;
         jsonObj.Add("type",QString("7").toStdString());
         jsonObj.Add("checkWord",sql->deal_CheckWord(word,sql->username,exp).toStdString());
         QString retData(jsonObj.ToString().c_str());
         tcp_send_receive->write(retData.toUtf8());
     }
}

